<?php

class Silk_Zgb_Model_Convert extends Mage_Catalog_Model_Product
{
	private $_headerMap = array(
		'sku'=>'SKU',
		'_store'=>'销售区域',
		'_product_websites'=>'销售区域',
		'_attribute_set'=>'',
		'_type'=>'',
		'_category'=>'分类',
		'_root_category'=>'',
		//'_product_websites'=>'销售区域',
		'brand'=>'品牌',
		'dealer_id'=>'经销商',
		'description'=>'介绍',
		'short_description'=>'简介',
		'manufacturer_id'=>'厂商',
		'name'=>'名称',
		'price'=>'价格',
		'special_price'=>'特价',
		'special_from_date'=>'特价起始日期',
		'special_to_date'=>'特价结束日期',
		'promotion'=>'促销信息',
		// 'short_description'=>'',
		'status'=>'状态',
		'unit'=>'单位',
		'visibility'=>'可见性',
		'warehouse_id'=>'仓库',
		// 'website_id'=>'',
		'image'=>'',
		'small_image'=>'',
		'thumbnail'=>'',
		'_media_image'=>'图片',
		'_media_attribute_id'=>'',
		'_media_position'=>'',
		'_media_is_disabled'=>'',
		'qty'=>'库存数量',
		'is_in_stock'=>'库存状况',
		'min_qty'=>'购物车里允许的最少数量',
		'max_sale_qty'=>'购物车数量上限'
	);

	private $_storeAttr = array(
			'special_price',
			'special_from_date',
			'special_to_date',
			'promotion',
			'dealer_id',
			'warehouse_id',
			'qty',
			'is_in_stock',
			'manufacturer_id',
			'min_qty',
			'max_sale_qty'
		);
	private $_globalAttr = array(
			'sku',
			'_attribute_set',
			'_type',
			'_category',
			'_root_category',
			'brand',
			'unit'
		);

	private $_headerIndex = array();
	private $_rows = array();

	public function processFile($path)
	{
		$reader = PHPExcel_IOFactory::createReader('Excel2007');
		$excel = $reader->load($path);
		$sheet = $excel->getSheet(0);

		return $this->parseSheet($sheet);
	}

	public function mapHeaderIndex($sheet)
	{
		$cols = $sheet->getHighestColumn();
		$rows = $sheet->getHighestRow();

		$this->_headerIndex = array();
		$_header = array();

		//创建index数组
		foreach($this->_headerMap as $key=>$head)
		{
			$this->_headerIndex[$key] = '';
			$_header[] = $key;
		}
		$this->_rows[] = $_header;

		//设置index数组
		for($row = 1; $row<= $rows; $row++)
		{
			for($col = 'A'; $col<= $cols; $col++)
			{
				$val = $sheet->getCell($col.$row)->getValue();
				foreach($this->_headerMap as $key=>$head)
				{
					if(isset($this->_headerIndex[$key]) && $head==$val)
					{
						$this->_headerIndex[$key] = $col;
						break;
					}
				}
			} 
		}
		//
		$this->_headerIndex['_product_websites'] = $this->_headerIndex['_store'];
		//print_r($this->_headerIndex);
	}

	public function parseSheet($sheet, $rows, $cols)
	{
		$rows = $sheet->getHighestRow();

		$this->mapHeaderIndex($sheet);


		$lines = array();
		for($row = 2; $row<= $rows; $row++)
		{
			$col = $this->_headerIndex['sku'];
			$sku = $sheet->getCell($col.$row)->getValue();
			if(!isset($lines[md5($sku)])) $lines[md5($sku)] = array();
			$lines[md5($sku)][] = $this->parseRow($sheet, $row);
		}
		//print_r($lines);
		//根据SKU处理商品
		return $this->parseSku($lines);
		return $this->_rows;
	}

	public function parseRow($sheet,$row)
	{
		//$cols = $sheet->getHighestColumn();
		// for($col = 'A'; $col<= $cols; $col++)
		// {

		// }
		//echo $sheet->getCell($col.$row)->getValue();
		$header = $this->_rows[0];
		$line = array();
		foreach($header as $attr)
		{
			$col = $this->getCol($attr);
			if(!$col) continue;
			$line[$col] = $sheet->getCell($col.$row)->getValue();
			//echo $col;
			//echo $col.$row."</br>";
			//$line[] = $this->parseAttr($idx, $attr, $sheet->getCell($col.$row)->getValue());
		}
		//$this->_rows[] = $line;
		//print_r($this->_rows);
		return $line;
	}

	public function getCol($code)
	{
		return $this->_headerIndex[$code];
	}

	public function parseSku($lines)
	{
		$data = array();
		foreach($lines as $skus)
		{
			$bunch = array();
			$images = array();
			$item = array();
			//先处理default
			$header = $this->_rows[0];
			$idx = 0;
			foreach($header as $attr)
			{
				$col = $this->getCol($attr);
				$item[] = $this->parseAttr($idx, $attr, $skus[0][$col]);
				if($attr == '_media_image')
				{
					$images = explode(';', $skus[0][$col]);
				}
			}
			//var_dump($images);
			//print_r($item);
			$bunch[] = $item;

			$idx++;
			foreach($skus as $line)
			{
				//print_r($line);
				$item = array();
				foreach($header as $attr)
				{
					$col = $this->getCol($attr);
					$item[] = $this->parseAttr($idx, $attr, $line[$col]);
				}
				$idx++;
				$bunch[] = $item;
			}

			//check and add padding
			if(count($images) > count($bunch))
			{
				$pad = count($images)-count($bunch);

				for($i=0;$i<$pad;$i++)
				{
					$item = array();
					foreach($header as $attr)
					{
						$item[] = '';
					}
					$bunch[] = $item;
				}
			}

			$mediaIdx = $this->getIndex('_media_image');
			
			//add image col
			for($i=0; $i<count($images); $i++)
			{
				$bunch[$i][$mediaIdx-3] = $images[0];
				$bunch[$i][$mediaIdx-2] = $images[0];
				$bunch[$i][$mediaIdx-1] = $images[0];
				$bunch[$i][$mediaIdx] = $images[$i];
				$bunch[$i][$mediaIdx+1] = 88;
				$bunch[$i][$mediaIdx+2] = $i+1;
				$bunch[$i][$mediaIdx+3] = 0;
			}

			$data = array_merge($data, $bunch);
			//break;
		}
		//print_r($this->_rows);
		//print_r($data);
		$this->_rows = array_merge($this->_rows, $data);
		//print_r($this->_rows);
		return $this->_rows;
	}

	public function getIndex($code)
	{
		$i = 0;
		foreach($this->_rows[0] as $attr)
		{
			if($code == $attr) return $i;
			$i++;
		}
		return 0;
	}

	public function parseAttr($idx, $code, $val)
	{
		$convert = Mage::helper('zgb/convert');
		if($idx != 0 && in_array($code, $this->_globalAttr)) return '';
		if($idx == 0 && in_array($code, $this->_storeAttr)) return '';
		//需要检查global属性和非global属性
		switch($code){
			case 'sku':
				return ($idx == 0?$val:'');
			case '_category':
				return "全部分类/{$val}";
			case '_root_category':
				return "根分类";
			case '_store':
				return $convert->getStore($idx, $val);
			case '_product_websites':
				return $convert->getWebsite($idx, $val);
			case '_attribute_set':
				return '百货商品';
			case '_type':
				return 'simple';
			case 'status':
				return 2;
			case 'visibility':
				return 1;
			case '_media_image':
				return '';
			default:
				return $val;
		}
	}
}